import {
  Body,
  Controller,
  Del,
  Get,
  Inject,
  Post,
  Put,
  Query,
} from '@midwayjs/core';
import { ArticleCategoryService } from '../../service/article_category/article_category.service';
import { Validate } from '@midwayjs/validate';
import { ListParamsValidator, StrIdValidator } from '../../validator';
import {
  EditArticleCategoryValidator,
  SaveArticleCategoryValidator,
} from '../../validator/article_category/article_category';
import { ArticleCategorySchema } from '../../schema';

@Controller('/article_category', { tagName: '' })
export class ArticleCategoryController {
  @Inject()
  article_categoryService: ArticleCategoryService;

  @Post('/list', { summary: '列表' })
  @Validate()
  async list(@Body() body: ListParamsValidator<ArticleCategorySchema>) {
    const data = await this.article_categoryService.list(body);
    return Object.assign(data, body);
  }

  @Post('/save', { summary: '保存' })
  @Validate()
  async save(@Body() body: SaveArticleCategoryValidator) {
    return await this.article_categoryService.save(body);
  }

  @Get('/info', { summary: '信息' })
  @Validate()
  async info(@Query() body: StrIdValidator) {
    return await this.article_categoryService.info(body.id);
  }

  @Del('/remove', { summary: '删除' })
  @Validate()
  async remove(@Query() body: StrIdValidator) {
    return await this.article_categoryService.remove(body.id);
  }

  @Del('/delete', { summary: '（真）删除' })
  @Validate()
  async realRemove(@Query() body: StrIdValidator) {
    return await this.article_categoryService.realRemove(body.id);
  }

  @Put('/edit', { summary: '编辑' })
  @Validate()
  async edit(
    @Query() query: StrIdValidator,
    @Body() body: EditArticleCategoryValidator
  ) {
    const article_category = await this.article_categoryService.info(query.id);
    Object.assign(article_category, body);
    return await this.article_categoryService.save(article_category);
  }

  @Get('/trees', { summary: '获取文章分类树结构' })
  @Validate()
  async trees() {
    const list = await this.article_categoryService.trees();
    return { list };
  }
}
